﻿<template>
  <div class="app-container">
    <div class="page-header">
      <el-form :inline="true" :model="listQuery" class="demo-form-inline" size="small">
        <el-form-item label="账号">
          <el-input v-model="listQuery.username" placeholder="账号" />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="listQuery.mobile" placeholder="手机号" />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="listQuery.email" placeholder="邮箱" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="user_status" style="width: 100px;">
            <el-option label="全部" :value="-1" />
            <el-option label="正常" :value="0" />
            <el-option label="禁用" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="editUser(null)">新增用户</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column prop="nickname" label="昵称" align="center" />
      <el-table-column prop="username" label="账号" align="center" />
      <el-table-column prop="mobile" label="手机号" align="center">
        <template slot-scope="scope">
          {{ scope.row.mobile ? scope.row.mobile : '/' }}
        </template>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" align="center">
        <template slot-scope="scope">
          {{ scope.row.email ? scope.row.email : '/' }}
        </template>
      </el-table-column>
      <el-table-column label="是否管理员" align="center" width="120">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.is_admin === 1 ? 'success' : 'danger'"
            size="small"
          >
            {{ scope.row.is_admin === 1 ? '是' : '否' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="状态" align="center" width="80">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 0 ? 'success' : 'danger'" size="small">
            {{ scope.row.status === 0 ? '正常' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间" width="160">
        <template slot-scope="scope">
          <span>{{ scope.row.created_at | dateFormat }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" width="160">
        <template slot-scope="scope">
          <el-button type="text" size="mini" icon="el-icon-edit" @click="editUser(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-if="paginate && paginate.total > 0"
      class="pagination-block"
      :current-page="paginate.currentPage"
      :page-size="paginate.perPage"
      :page-sizes="[1, 20, 30, 40, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="paginate.total"
      background
      @size-change="paginate.handleSizeChange"
      @current-change="paginate.handleCurrentChange"
    />
    <!--添加修改-->
    <user :show="drawerShow" :first-row="currentRow" />
  </div>
</template>

<script>
import api from '@/api/'
import user from './edit/user'

export default {
  components: {
    user
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  provide() {
    return {
      app: this
    }
  },
  data() {
    return {
      drawerShow: false,
      list: null,
      loading: true,
      currentRow: null,
      paginate: {
        perPage: 20,
        total: 0,
        currentPage: 1,
        // 点击页码事件
        handleSizeChange: (perPage) => {
          this.listQuery.perPage = perPage
          this.getUsers()
        },
        handleCurrentChange: (currentPage) => {
          this.listQuery.page = currentPage
          this.getUsers()
        }
      },
      user_status: -1,
      listQuery: {
        page: 1,
        perPage: 20
      }
    }
  },
  created() {
    this.getUsers()
  },
  methods: {
    getUsers() {
      this.listLoading = true
      if (this.user_status >= 0) {
        this.listQuery.status = this.user_status
      }
      api.getUserList(this.listQuery).then(response => {
        this.list = response.item
        this.paginate.total = response.total
        this.paginate.currentPage = response.currentPage
        this.paginate.perPage = response.perPage
      }).catch(() => {
      })
      this.listLoading = false
    },
    onSubmit() {
      this.getUsers()
    },
    editUser(row) {
      this.drawerShow = true
      if (row === null) {
        this.currentRow = {}
      } else {
        this.currentRow = row
      }
    },
    deleteUser(id) {
      console.log(id)
    }
  }
}
</script>
